<?php 
class GoodsModel extends Model{
	public $table="goods";
	public $validate=array(
	array("gname","nonull","商品名称不能为空",2,3),
	array("gnumber","num:1,65535","商品货号必须是1到65535之间的一个数",2,3),
	array("unit","nonull","商品单位不能为空",2,3),
	array("marketprice","num:0,100000000","市场价格必须为0到100000000之间的数字",2,3),
	array("shopprice","num:0,100000000","市场价格必须为0到100000000之间的数字",2,3),
	array("category_id","nonull","请选择所属分类",2,3),
	array("brand_id","nonull","请选择所属品牌",2,3),
	array("brand_id","nonull","请选择所属品牌",2,3)
	);
	
	public function addData(){
		if(!$this->create()){
			return false;
		}
		
//		验证商品详情表
		$model=K("Details");
		if(!$model->create()){
			$this->error=$model->error;
			return false;
		}
//		验证商品属性表
		$gamodel=K("GoodsAttr");
		if(!$gamodel->create()){
			$this->error=$gamodel->error;
			return false;
		}
//		
        
		$gid=$this->add();
		$shoppic=$_POST['shopPic'];
		$shoppic=implode(',',$shoppic);
		$data=array('goods_gid'=>$gid,'details'=>$_POST['details'],'shoppic'=>$shoppic);
		
		
//      添加详情表

	    $model->addData($data);
		$gamodel->addData($gid);
		
		return $gid;
		
	}
	public function updateData($gid){
		
		if(!$this->create())return false;
		
//		验证商品详情表
		$model=K('Details');
		if(!$model->create()){
			$this->error=$model->error;
			return false;
		}
//		验证商品属性表
        $gamodel=K('GoodsAttr');
		if(!$gamodel->create()){
			$this->error=$gamodel->error;
			return false;
		}
		$gid=$this->data['gid'];
	
		$this->where("gid='{$gid}'")->update();
		$model->where("goods_gid='{$gid}'")->update();
		$gamodel->updateData($gid);
        return true;
		
		
	}
	public function delData($gid){
		$this->where("gid='{$gid}'")->delete();
		$model=K("Details");
		$model->where("goods_gid='{$gid}'")->delete();
		$gamodel=K("GoodsAttr");
		$gamodel->where("goods_gid='{$gid}'")->delete();
		
		
	}
}
	
	
	
	?>